package com.game.sprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class SpriteAnimated extends Sprite
{
	/**
	 * Imagem do sprite
	 */
	//private Bitmap bitmap;	
	/**
	 * Ret�ngulo de recorte dos frames
	 */
	private Rect sourceRect;	
	/**
	 * N�mero de frames da anima��o
	 */
	private int frameNumber;	// = frameCount
	/**
	 * Frame atual da anima��o
	 */
	private int currentFrame;	
	/**
	 * Intervalo entre os frames
	 */
	private long frameTicker;	
	/**
	 * Frames por milisegundo
	 */
	private int framePeriod; // = fps
	
	/**
	 * Largura e altura do frame
	 */
	private int spriteWidth;
	private int spriteHeight;
	
	/**
	 * Posi��o do frame na tela
	 */
	//private int x;
	//private int y;
	
	public SpriteAnimated(Bitmap bitmap, int x,	int y, int width, int height, int fps, int frameCount)
	{
		super(bitmap,x,y);
		//this.bitmap = bitmap;
	//	this.x = x;
	//	this.y = y;
		currentFrame = 0;
		frameNumber = frameCount;   //Atributo = parametro
		spriteWidth = bitmap.getWidth() / frameCount;
		spriteHeight = bitmap.getHeight();
		sourceRect = new Rect(
				0,
				0,
				spriteWidth,
				spriteHeight);
		framePeriod = 1000 / fps;
		frameTicker = 0l;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public Rect getSourceRect() {
		return sourceRect;
	}

	public void setSourceRect(Rect sourceRect) {
		this.sourceRect = sourceRect;
	}

	public int getFrameNumber() {
		return frameNumber;
	}

	public void setFrameNumber(int frameNumber) {
		this.frameNumber = frameNumber;
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}

	public long getFrameTicker() {
		return frameTicker;
	}

	public void setFrameTicker(long frameTicker) {
		this.frameTicker = frameTicker;
	}

	public int getFramePeriod() {
		return framePeriod;
	}

	public void setFramePeriod(int framePeriod) {
		this.framePeriod = framePeriod;
	}

	public int getSpriteWidth() {
		return spriteWidth;
	}

	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}

	public int getSpriteHeight() {
		return spriteHeight;
	}

	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}	
	
	public void Update(long gameTime)
	{
		if(gameTime > frameTicker + framePeriod)
		{
			frameTicker = gameTime;
			
			currentFrame++;
			
			if (currentFrame >= frameNumber)
			{
				currentFrame = 0;
			}
		}
		
		this.sourceRect.left = currentFrame * spriteWidth;
		this.sourceRect.right = this.sourceRect.left + spriteWidth;
	}
	
	public void draw(Canvas canvas)
	{
		Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);
		
		canvas.drawBitmap(bitmap, sourceRect, destRect, null);
	}

}
